package com.API.dal.providers;

import com.API.dal.entities.Objects;
import com.API.dal.entities.ObjectsTypes;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public class SupplierProviderImp implements SupplierProvider {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void addSupplier(Objects supplier) {
        ObjectsTypes ot;
        Session session  = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from ObjectsTypes ot where ot.name = 'Поставщик'");
        ot = (ObjectsTypes) query.uniqueResult();
        supplier.setObjectsTypes(ot);
        session.save(supplier);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
    public void updateSupplier(Objects supplier) {
        Session session = sessionFactory.getCurrentSession();
        session.update(supplier);
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Objects> getProjectSuppliers(int project_id) {
        List<Objects> supplierList;
        Session session =sessionFactory.getCurrentSession();
        Query query = session.createQuery(
                "from Objects ob where ob.id " +
                "in (select ord.objectsByProviderId.id from Orders ord " +
                        " where ord.objectsByProjectId.id = " + project_id + ")");
        supplierList = query.list();
        return supplierList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Objects> getProjectSuppliers(String project_name) {
        List<Objects> supplierList;
        Session session =sessionFactory.getCurrentSession();
        Query query = session.createQuery(
                "from Objects ob where ob.id " +
                        "in (select ord.objectsByProviderId.id from Orders ord " +
                        " where ord.objectsByProjectId.name = " + project_name + ")");
        supplierList = query.list();
        return supplierList;
    }

    @Override
    @Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
    public List<Objects> getLocationSuppliers(String location_name) {
        List<Objects> supplierList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects ob " +
                "where ob.objectsTypes.name = 'Поставщик' " +
                "and ob.location.name = " + location_name);
        supplierList = query.list();
        return supplierList;
    }

    @Override
    @Transactional(readOnly=true,propagation= Propagation.SUPPORTS)
    public List<Objects> getLocationSuppliers(int location_id) {
        List<Objects> supplierList;
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery("from Objects ob " +
                "where ob.objectsTypes.name = 'Поставщик' " +
                "and ob.location.id = " + location_id);
        supplierList = query.list();
        return supplierList;
    }
}
